package com.lnking.lnkingplace.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lnking.lnkingplace.Util.LocalDateTimeUtil;
import com.lnking.lnkingplace.mapper.IsolationPlaceMapper;
import com.lnking.lnkingplace.mapper.RecordMedicalWasteSterilizeMapper;
import com.lnking.lnkingplace.model.dto.record.medicalWasteSterilize.MedicalWasteSterilizeImportDto;
import com.lnking.lnkingplace.model.entity.RecordMedicalWasteSterilize;
import com.lnking.lnkingplace.model.enums.DateFormat;
import com.lnking.lnkingplace.model.request.record.medicalWasteSterilize.MedicalWasteSterilizeAddRequest;
import com.lnking.lnkingplace.model.request.record.medicalWasteSterilize.MedicalWasteSterilizeUpdateRequest;
import com.lnking.lnkingplace.service.IRecordMedicalWasteSterilizeService;
import com.lnking.starter.export.utils.ImportOption;
import com.lnking.starter.export.utils.ImportUtils;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangqt
 * @since 2022-09-29
 */
@Service
public class RecordMedicalWasteSterilizeServiceImpl extends CustomServiceImpl<RecordMedicalWasteSterilizeMapper, RecordMedicalWasteSterilize> implements IRecordMedicalWasteSterilizeService {

    @Resource
    IsolationPlaceMapper isolationPlaceMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file) throws Exception {
        ImportOption option = new ImportOption.Builder().build();
        List<MedicalWasteSterilizeImportDto> result =  ImportUtils.importExcel(file, MedicalWasteSterilizeImportDto.class,option);
        result.forEach(r ->{
            if(StringUtils.isNotBlank(r.getPlaceName())){
                RecordMedicalWasteSterilize info = new RecordMedicalWasteSterilize();
                BeanUtils.copyProperties(r,info);
                //查询placeId
                String placeId = isolationPlaceMapper.getPlaceId(r.getPlaceName());
                Assert.isTrue(StringUtils.isNotBlank(placeId), "该场所不存在："+"【"+r.getPlaceName()+"】");
                info.setPlaceId(placeId);
                info.setAirStartTime(LocalDateTimeUtil.stringToLocalDateTime(r.getAirStartTime(), DateFormat.LOCAL_DATE_TIME));
                info.setAirEndTime(LocalDateTimeUtil.stringToLocalDateTime(r.getAirEndTime(),DateFormat.LOCAL_DATE_TIME));
                info.setObjectStartTime(LocalDateTimeUtil.stringToLocalDateTime(r.getObjectStartTime(),DateFormat.LOCAL_DATE_TIME));
                info.setCreateTime(LocalDateTime.now());
                info.setTemperature(Integer.parseInt(r.getTemperature()));
                info.setHumidity(Integer.parseInt(r.getHumidity()));
                info.setTotalTime(Integer.parseInt(r.getTotalTime()));
                this.baseMapper.insert(info);
            }
        });
    }

    @Override
    public void add(MedicalWasteSterilizeAddRequest params) {
        RecordMedicalWasteSterilize info = new RecordMedicalWasteSterilize();
        BeanUtils.copyProperties(params,info);
        info.setCreateTime(LocalDateTime.now());
        info.setPlaceId(isolationPlaceMapper.getPlaceId(params.getPlaceName()));
        this.baseMapper.insert(info);
    }

    @Override
    public void update(MedicalWasteSterilizeUpdateRequest params) {
        RecordMedicalWasteSterilize info = new RecordMedicalWasteSterilize();
        BeanUtils.copyProperties(params,info);
        info.setPlaceId(isolationPlaceMapper.getPlaceId(params.getPlaceName()));
        this.baseMapper.updateById(info);
    }
}
